//
//  ViewController.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/9/21.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#import "ZWYNetTool.h"
#import "FoundDataModel.h"
#import "FoundTopicsModel.h"
#import "FoundUserModel.h"
#import "FoundTagsModel.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,UISearchBarDelegate>
@property(nonatomic,strong)FoundDataModel *model;
@property(nonatomic,strong)NSMutableArray *titleArray;
@property(nonatomic,strong)NSMutableArray *Array;
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,assign)NSInteger number;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArray = [NSMutableArray array];
    _Array = [NSMutableArray array];
    _mySearchBar.delegate = self;
//    [self netWork];
    self.navigationController.navigationBar.hidden = YES;
    [_noButton addTarget:self action:@selector(back) forControlEvents:(UIControlEventTouchUpInside)];
    [_mySegment addTarget:self action:@selector(segment) forControlEvents:(UIControlEventValueChanged)];
    _myScrollView.delegate = self;
    _myScrollView.contentSize = CGSizeMake(2 * self.view.bounds.size.width, self.view.bounds.size.height - 99);
    _myScrollView.contentOffset = CGPointMake(0, 99);
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0 * self.view.bounds.size.width, 99, self.view.bounds.size.width, _myScrollView.bounds.size.height) style:(UITableViewStylePlain)];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"1"];
    [_myScrollView addSubview:_tableview];

}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [self netWork];
}
- (void)netWork {
    [_titleArray removeAllObjects];
    [ZWYNetTool GET:@"http://api.kuaikanmanhua.com/v1/topic_new/lists/get_by_tag?count=20&since=0&tag=0" andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.model = [FoundDataModel modelWithDic:result[@"data"]];
        for (int i = 0; i < self.model.topics.count; i ++) {
            NSLog(@"%@",self.model.topics);
            if ([self.model.topics[i].title rangeOfString:_mySearchBar.text].location != NSNotFound) {
                NSString *title = self.model.topics[i].title;
                [_titleArray addObject:title];
            }
            if ([self.model.topics[i].user.nickname rangeOfString:_mySearchBar.text].location != NSNotFound) {
                NSString *title = self.model.topics[i].user.nickname;
                [_Array addObject:title];
            }
        }
        NSLog(@"=====================%@",_titleArray);
        [_tableview reloadData];
        
        
    } andFailureBlock:^(NSError *error) {
        
        
    }];
}
- (void)segment {
    if (_mySegment.selectedSegmentIndex == 0) {
        _number = _mySegment.selectedSegmentIndex;
        [_tableview reloadData];
    } else if (_mySegment.selectedSegmentIndex == 1) {
        _number = _mySegment.selectedSegmentIndex;
        [_tableview reloadData];
    }
}
- (void)back {
//    [self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_number == 0) {
        if (_titleArray.count != 0) {
            return _titleArray.count;
        } else {
            return 1;
        }
    } else {
        if (_Array.count != 0) {
            return _Array.count;
        } else {
            return 1;
        }
    }
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1" forIndexPath:indexPath];
    if (_number == 0) {
        if (_titleArray.count != 0) {
            cell.textLabel.text = [NSString stringWithFormat:@"%@",_titleArray[indexPath.row]];
            NSLog(@"~~~~~~~~~~~~~~~~~~~~%@",_titleArray[indexPath.row]);
            NSLog(@"==========%@",cell.textLabel.text);
            
        } else {
            cell.textLabel.text = @"别瞅了，这啥也没有";
            
        }
    } else {
        if (_Array.count != 0) {
            cell.textLabel.text = [NSString stringWithFormat:@"%@",_Array[indexPath.row]];
            NSLog(@"~~~~~~~~~~~~~~~~~~~~%@",_Array[indexPath.row]);
            NSLog(@"==========%@",cell.textLabel.text);
            
        } else {
            cell.textLabel.text = @"别瞅了，这啥也没有";
            
        }
    }
    
   
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
